package com.appkubes.fintech.admin.application.risk.data.boole.utils;

import com.appkubes.fintech.admin.application.risk.data.boole.utils.interceptor.RetryIntercepter;
import lombok.extern.slf4j.Slf4j;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author: wuzhijian
 * @Date: 2019/12/25
 * @Description: 布尔数据加密工具类
 */
@Slf4j
@Configuration
public class BooleApiEncryptionUtil {

    @Autowired
    private Environment environment;


    /**
     * 唯一客户编号
     */
    private static String institutionId;

    /**
     * aes密钥
     */
    private static String aesKey;

    /**
     * rsa私钥
     */
    private static String rsaPrivateKey;


    @PostConstruct
    public void init() {
        institutionId = environment.getProperty("boole.data.institutionId");
        aesKey = environment.getProperty("boole.data.aesKey");
        rsaPrivateKey = environment.getProperty("boole.data.rsaPrivateKey");
    }


//    public static Map<String, Object> getRequestMap(Map<String, Object> customData, String version) throws Exception {
//        log.info("");
//        //对业务数据字段 biz_data 明文数据 MEDIATYPE_JSON 化
//        String js = RSA.getSignContent(new JSONObject(customData));
//        // RSA 加密 使用私钥进行加密
//        String signVal = RSA.sign(js, rsaPrivateKey);
//        // AES 加密
//        String bizDataRaw = JSONObject.toJSON(customData).toString();
//        String bizDataEncoded = AES.encrypt(bizDataRaw, aesKey);
//        String signEncoded = URLEncoder.encode(signVal);
//        String bizDataEncoded2 = URLEncoder.encode(bizDataEncoded);
//        BaseRiskManagementRequest riskManagementRequest = new BaseRiskManagementRequest();
//        riskManagementRequest.setBizData(bizDataEncoded2);
//        riskManagementRequest.setInstitutionId(institutionId);
//        riskManagementRequest.setSign(signEncoded);
//        riskManagementRequest.setTimestamp(String.valueOf(System.currentTimeMillis()));
//        riskManagementRequest.setVersion(version);
//        return MapBeanUtil.beanToMapUnderline(riskManagementRequest);
//    }

    public static Response postWithRetry(String url, Map<String, Object> params) throws IOException {
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(800, TimeUnit.MILLISECONDS)
                .readTimeout(2500, TimeUnit.MILLISECONDS)
                //重试
                .addInterceptor(new RetryIntercepter(3))
                .build();
        List<String> nameList = new ArrayList<>();
        List<String> valueList = new ArrayList<>();
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            Object value = entry.getValue();
            if (value != null) {
                nameList.add(entry.getKey());
                valueList.add(entry.getValue().toString());
            }
        }
        //提交形式 form-data 格式请求：Content-Type：multipart/form-data
        FormBody formBody = new FormBody(nameList, valueList);
        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();
        return client.newCall(request).execute();
    }

}
